package com.sefer.dragonfly.client.confige;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.digester.Rule;

public class PluginAddRule extends Rule {

    protected String methodName = null;

    protected String paramType  = null;

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getParamType() {
        return paramType;
    }

    public void setParamType(String paramType) {
        this.paramType = paramType;
    }

    @Override
    public void end() throws Exception {
        // Identify the objects to be used
        Object child = digester.peek(0);
        Object parent = digester.peek(1);

        // Call the specified method
        Class paramTypes[] = new Class[1];
        if (paramType != null) {
            paramTypes[0] = digester.getClassLoader().loadClass(paramType);
        } else {
            paramTypes[0] = child.getClass();
        }

        MethodUtils.invokeMethod(parent, methodName, new Object[] { child }, paramTypes);

        // clear the top object after add to the parent
        digester.pop();
    }

    public PluginAddRule(String methodName, String paramType) {
        super();
        this.methodName = methodName;
        this.paramType = paramType;
    }

}
